#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.basic.module.element.service.impl;

import ${package}.basic.module.element.mapper.ElementMapper;
import ${package}.basic.module.element.service.ElementService;
import ${package}.basic.module.permission.mapper.PermissionMapper;
import ${package}.common.bean.po.Element;
import ${package}.common.bean.po.ElementPermission;
import ${package}.common.bean.vo.Constraint;
import ${package}.common.bean.vo.QueryBean;
import ${package}.common.emum.ElementStatusEnum;
import ${package}.common.emum.ReturnCodeEnum;
import ${package}.common.exception.ValidException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 无法补充
 *
 * @author zhang wen yi
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Service
@AllArgsConstructor
public class ElementServiceImpl implements ElementService{
    /**
     * 元素Mapper
     */
    private ElementMapper elementMapper;
    /**
     * 权限Mapper
     */
    private PermissionMapper permissionMapper;


    /**
     * Description:
     * <查询页面元素列表>
     * @author pig
     * @date 16:31 2019/8/6
     * @param queryBean 1
     * @return com.github.pagehelper.PageInfo<Element>
     **/
    @Override
    public PageInfo<Element> queryElementByLimit(QueryBean queryBean){

        // 筛选状态
        Constraint constraint = Constraint.build()
                .putField("e.status")
                .putOp(Constraint.LOPERATE.IN.getOperation())
                .putValus(Boolean.TRUE, ElementStatusEnum.ENABLED.getCode(), ElementStatusEnum.DISABLE.getCode())
                .putValueType(Constraint.DATATYPE.STRING.getType());
        queryBean.and(constraint);
        Page<Element> page = PageMethod.startPage(queryBean.getCurrent(), queryBean.getPageSize());
        elementMapper.queryElementByLimit(queryBean);

        return new PageInfo<>(page);
    }

    /**
     * Description:
     * <新增页面元素>
     * @author pig
     * @date 16:31 2019/8/6
     * @param element 1
     **/
    @Override
    public void addElement(Element element){

        elementMapper.addElement(element);

    }

    /**
     * Description:
     * <根据主键修改页面元素>
     * @author pig
     * @date 16:31 2019/8/6
     * @param element 1
     **/
    @Override
    public void updateElementByKey(Element element){

        elementMapper.updateElementByKey(element);
    }

    /**
     * Description:
     * <根据主键查询页面元素信息>
     * @author pig
     * @date 16:31 2019/8/6
     * @param id 1
     * @return Element
     **/
    @Override
    public Element queryElementByKey(Long id) {
        return elementMapper.queryElementByKey(id);
    }

    /**
     * Description:
     * <根据主键删除页面元素>
     * @author pig
     * @date 16:32 2019/8/6
     * @param id 1
     **/
    @Override
    public void deleteElementByKey(Long id){

        //是否有角色占用
        int i = permissionMapper.queryElementPremissionCountByElementKey(id);

        if (i !=0){
            throw new ValidException(ReturnCodeEnum.CURRENT_ELEMENT_WAS_OCCUPIED);
        }

        elementMapper.deleteElementByKey(id);
    }

    /**
     * Description:
     * <批量删除页面元素>
     * @author pig
     * @date 16:32 2019/8/6
     * @param keys 1
     * @return java.util.List<Element>
     **/
    @Override
    public List<Element> removeElementsBykeys(List<Long> keys) {
        // 区分被角色占用的页面元素权限
        List<Long> occupied = new ArrayList<>();
        List<Long> free = new ArrayList<>();

        // 判断是否有角色占用页面元素权限
        List<ElementPermission> permissions = permissionMapper.findElementPermissionsByElementKeys(keys);
        for (Long key: keys) {
            boolean flag = false;
            for (ElementPermission permission: permissions) {
                if (key.equals(permission.getElementId())){
                    flag = true;
                    break;
                }
            }
            if (flag){
                occupied.add(key);
            }else {
                free.add(key);
            }
        }
        // 将页面元素状态置为删除
        if (CollectionUtils.isNotEmpty(free)){
            elementMapper.updateElementsStatusDeleteBykeys(free);
        }
        // 查询被占用的页面元素
        if (CollectionUtils.isNotEmpty(occupied)){
            return elementMapper.findElementsByKeys(occupied);
        }else {
            return Lists.newArrayList();
        }

    }

    /**
     * Description:
     * <禁用/启用>
     * @author pig
     * @date 16:33 2019/8/6
     * @param element 1
     **/
    @Override
    public void disableOrEnableElement(Element element){
        //是否有角色占用
        int i = permissionMapper.queryElementPremissionCountByElementKey(element.getId());

        if (i !=0){
            throw new ValidException(ReturnCodeEnum.CURRENT_ELEMENT_WAS_OCCUPIED);
        }

        //判断传来的状态更新为相反状态
        if(element.getStatus().intValue() == ElementStatusEnum.ENABLED.getCode().intValue()){
            element.setStatus(ElementStatusEnum.DISABLE.getCode());
        }else {
            element.setStatus(ElementStatusEnum.ENABLED.getCode());
        }

        elementMapper.updateElementDisableOrEnable(element);
    }

    /**
     * Description:
     * <查询编码是否重复>
     * @author pig
     * @date 16:33 2019/8/6
     * @param code 1
     * @param id 2
     * @return java.lang.Boolean
     **/
    @Override
    public Boolean elementVerifyCode(String code, Long id){
        if (id == null) {
	        return CollectionUtils.isEmpty(elementMapper.findElementsByCode(code));
        } else {
	        return CollectionUtils.isEmpty(elementMapper.findElementsByCodeOrNotInId(new Element(id, code)));
        }
    }
}
